package com.gragra.sampling.vector.tests;
import static org.junit.Assert.*;
import java.util.Arrays;
import org.junit.Before;
import org.junit.Test;
import com.gragra.sampling.ThreadedUniformAccess;
import com.gragra.sampling.vector.LimitedClassesUnitCRP;
import com.gragra.sampling.vector.VectorStructure;
import com.gragra.util.StringToInt;
/**
 * 
 * @author Christoph Teichmann
 * 14.10.2013
 */
public class TestVectorStructure
{
	/**
	 * 
	 */
	private ThreadedUniformAccess tua;
	/**
	 * 
	 */
	private LimitedClassesUnitCRP lcc;
	/**
	 * 
	 */
	private VectorStructure vs1;
	/**
	 * 
	 */
	private VectorStructure vs2;
	@Before
	public void setUp() throws Exception
	{
		vs1 = new VectorStructure(StringToInt.ensure("Hans"), new double[] {5.0,4.5,7.2,2.1}, new int[][] {{0,0},{1,0},{1,1},{2,3}}, 3);
		vs2 = new VectorStructure(StringToInt.ensure("met"), new double[] {8.0,6.3,11.2,3.0}, new int[][] {{0,1},{1,0},{1,4},{2,5}}, 3);
		lcc = new LimitedClassesUnitCRP(3, new double[] {0.1,0.4,5.0}, 0.5, 3);
		tua = new ThreadedUniformAccess(3);
	}
	@Test
	public void testSetLabel()
	{
		vs1.setLabel(1, 0);
		vs1.setLabel(5, 1);
		vs1.setLabel(17, 2);
		vs1.setBest(1);
		assertEquals(vs1.getBest(lcc),"Hans : 5");
	}
	@Test
	public void testGetCodes()
	{
		int[][] arr = vs2.getCodes();
		assertEquals(arr[2][0],1);
		assertEquals(arr[2][1],4);
		assertEquals(arr[3][0],2);
		assertEquals(arr[3][1],5);
	}
	@Test
	public void testGetName()
	{assertEquals(StringToInt.ensure("met"),vs2.getName());}
	@Test
	public void testGetLength()
	{assertEquals(vs1.getLength(),4);}
	@Test
	public void testGetLabelCount()
	{
		vs1.setLabel(1, 0);
		vs1.setLabel(5, 1);
		vs1.setLabel(17, 2);
		vs1.addSample();
		vs1.addSample();
		assertEquals(vs1.getLabelCount(17),2.0,0.0);
	}
	@Test
	public void testGetValues()
	{assertEquals(Arrays.toString(vs2.getValues()),"[8.0, 6.3, 11.2, 3.0]");}
	@Test
	public void testGetLabel()
	{
		vs1.setLabel(1, 0);
		vs1.setLabel(5, 1);
		vs1.setLabel(17, 2);
		assertEquals(vs1.getLabel(1),5);
	}
	@Test
	public void testGetBestIntCode()
	{
		vs1.setLabel(1, 0);
		vs1.setLabel(5, 1);
		vs1.setLabel(17, 2);
		vs1.setBest(2);
		assertEquals(vs1.getBestIntCode(),17);
	}
	@Test
	public void testResample()
	{
		for(int i=0;i<500;++i)
		{
			for(int thread=0;thread<3;++thread)
			{vs1.resample(lcc, 2.0, thread, tua);}
			vs1.addSample();
		}
		assertEquals((vs1.getLabelCount(0))/(vs1.getLabelCount(1)),1.0,0.3);
		assertEquals((vs1.getLabelCount(1))/(vs1.getLabelCount(0)),1.0,0.3);
		assertEquals((vs1.getLabelCount(2))/(vs1.getLabelCount(1)),1.0,0.3);
		assertEquals((vs1.getLabelCount(2))/(vs1.getLabelCount(0)),1.0,0.3);
		assertEquals((vs1.getLabelCount(1))/(vs1.getLabelCount(2)),1.0,0.3);
		assertEquals((vs1.getLabelCount(0))/(vs1.getLabelCount(2)),1.0,0.3);
	}
	@Test
	public void testMinRisk()
	{
		vs1.setLabel(1, 0);
		vs1.setLabel(17, 1);
		vs1.setLabel(17, 2);
		vs1.addSample();
		assertEquals(vs1.minRisk(lcc),"Hans : 17");
	}
	@Test
	public void testInitialize()
	{
		vs2.setLabel(1, 0);
		lcc.add(vs2, 0);
		lcc.add(vs2, 0);
		lcc.add(vs2, 0);
		lcc.add(vs2, 0);
		lcc.add(vs2, 0);
		lcc.add(vs2, 0);
		lcc.add(vs2, 0);
		lcc.add(vs2, 0);
		vs1.initialize(lcc, tua);
		lcc.add(vs1, 0);
		lcc.add(vs1, 1);
		lcc.add(vs1, 2);
		vs2.initialize(lcc, tua);
		assertEquals(vs2.getLabel(0),1);
		assertTrue(vs1.getLabel(0) != 1);
	}
}